﻿using bonus.Models;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace bonus.Controllers
{
	public class LoginController : Controller
	{
		/// <summary>
		/// 登录页面
		/// </summary>
		/// <returns></returns>
		public ActionResult Index()
		{
			return View();
		}

		/// <summary>
		/// 提交登录信息
		/// </summary>
		/// <param name="name">用户名称</param>
		/// <param name="pwd">用户密码</param>
		/// <returns>登录结果</returns>
		[HttpPost]
		public JsonResult Index(string name, string pwd, string code)
		{
			if (string.IsNullOrEmpty(code) || code != Session["ValidateCode"].ToString())
			{
				return Json(new { Code = "FAIL", Msg = "验证码错误" });
			}

			Repository.Repository tool = new Repository.Repository();

			HttpCookie cookie = new HttpCookie(WebConfig.USERCOOKIE);

			var filter = Builders<Models.User>.Filter.Eq("NO", name);

			var entity = tool.GetOne(filter);

			if (entity == null)
			{
				return Json(new { Code = "FAIL", Msg = "用户名错误" });
			}

			if (!Repository.FuBeiMD5.Verify(pwd, entity.Pwd1, WebConfig.KEY, WebConfig.UFT8))
			{
				return Json(new { Code = "FAIL", Msg = "密码错误" });
			}
			cookie.Values.Add("UserName", HttpUtility.UrlEncode(entity.Name));

			cookie.Values.Add("NO", entity.NO);

			cookie.Values.Add("UserId", entity._id.ToString());

			cookie.Values.Add("Type", Repository.FuBeiMD5.Sign(entity.UserType.ToString(), Models.WebConfig.KEY, Models.WebConfig.UFT8));

			cookie.Values.Add("Level", entity.Level.ToString());

			cookie.Values.Add("Money", entity.Money.ToString("0.##"));

			cookie.Expires = DateTime.Now.AddDays(10);

			byte[] byteArray = Encoding.Default.GetBytes(cookie.Value);

			cookie.Value = HttpUtility.UrlEncode(byteArray).ToString();

			System.Web.HttpContext.Current.Response.AppendCookie(cookie);

			return Json(new { Code = "OK", Msg = "登录成功，正在跳转" });
		}

		/// <summary>
		/// 获取验证码
		/// </summary>
		/// <returns></returns>
		public ActionResult CheckCode()
		{
			//生成验证码
			ValidateCode validateCode = new ValidateCode();
			string code = validateCode.CreateValidateCode(4);
			Session["ValidateCode"] = code;
			byte[] bytes = validateCode.CreateValidateGraphic(code);
			return File(bytes, @"image/jpeg");
		}

		/// <summary>
		/// 退出
		/// </summary>
		/// <returns></returns>
		public ActionResult LoginOut()
		{
			if (Request.Cookies[Models.WebConfig.USERCOOKIE] != null)
			{
				HttpCookie _cookie = Request.Cookies[Models.WebConfig.USERCOOKIE];
				_cookie.Expires = DateTime.Now.AddHours(-1);
				Response.Cookies.Add(_cookie);
			}

			return RedirectToAction("Index");
		}


		//public void Test()
		//{


		//	for (int i = 0; i < 1000000; i++)
		//	{
		//		Models.Order order = new Order();

		//		order.Add = "收货地址";
		//		order.AddDT = new DateTime(2017, 10, 1);
		//		order.AddPC = "123465";
		//		order.Address = "详细地址";
		//		order.BuyType = 1;
		//		order.Mobile = "13007607662";
		//		order.Money = 100;
		//		order.OrderDT = new DateTime(2017, 10, 1);
		//		order.OrderNo = "123456789";
		//		order.PayDT = new DateTime(2017, 10, 1);
		//		order.PV = 100;
		//		order.Remarks = "备注";
		//		order.SaleName = "那么";
		//		order.SaleNo = "123456865";
		//		order.UserName = "12df";
		//		order._id = ObjectId.GenerateNewId();


		//		Repository.Repository tool = new Repository.Repository();

		//		var result = tool.AddOneAsync(order);
		//	}


		//}


	}


}